package com.young.jdbc.myjdbc;

import com.mysql.jdbc.Driver;
import org.junit.Test;

import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/**
 * @Author: young
 * @Date: 2021-12-02 21:35
 * @Description: 数据库的连接方式
 */
public class Jdbc_demo {
    // 方式一
    @Test
     public void connect01() throws SQLException {
        // 1.注册驱动
        Driver driver = new Driver();
        // 2.得到连接
        String url = "jdbc:mysql://localhost:3306/young";
        Properties properties = new Properties();
        properties.setProperty("user","root");
        properties.setProperty("password","root");
        Connection connect = driver.connect(url, properties);
        // 3.执行SQL
        String sql = "insert into student values(null,'young','男','1999-9-9','666666')";
        // statement 用于执行静态SQL语句并返回其生成的结果
        Statement statement = connect.createStatement();
        int rows = statement.executeUpdate(sql);    // 是dml语句，返回的是影响的行数
        System.out.println(rows>0 ? "成功" : "失败");
        // 4.关闭连接资源
         statement.close();
         connect.close();
     }
    // 方式二：使用反射加载Driver类;动态加载，更加灵活，减少依赖性
    @Test
    public void connect02() throws Exception{
        // 1.注册驱动
        // 使用反射加载Driver类;动态加载，更加灵活，减少依赖性
        Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
        Driver driver = (Driver) aClass.newInstance();
        // 2.得到连接
        String url = "jdbc:mysql://localhost:3306/young";
        Properties properties = new Properties();
        properties.setProperty("user","root");
        properties.setProperty("password","root");
        Connection connect = driver.connect(url, properties);
        // 3.执行SQL
        String sql = "insert into student values(null,'young','男','1999-9-9','666666')";
        // statement 用于执行静态SQL语句并返回其生成的结果
        Statement statement = connect.createStatement();
        int rows = statement.executeUpdate(sql);    // 是dml语句，返回的是影响的行数
        System.out.println(rows > 0 ? "成功" : "失败");
        // 4.关闭连接资源
        statement.close();
        connect.close();
        }
    // 方式三：使用DriverManager替代Driver镜像统一管理
    @Test
    public void connect03() throws Exception{
        // 使用反射加载Driver类
        Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
        Driver driver = (Driver) aClass.newInstance();
        // 创建url，user，password
        String url = "jdbc:mysql://localhost:3306/young";
        String user = "root";
        String password = "root";
        // 注册驱动
        DriverManager.registerDriver(driver);
        Connection connection = DriverManager.getConnection(url, user, password);
        // 3.执行SQL
        String sql = "insert into student values(null,'小小','女','1999-8-8','555555')";
        // statement 用于执行静态SQL语句并返回其生成的结果
        Statement statement = connection.createStatement();
        int rows = statement.executeUpdate(sql);    // 是dml语句，返回的是影响的行数
        System.out.println(rows > 0 ? "成功" : "失败");
        // 4.关闭连接资源
         statement.close();
        connection.close();
    }
    // 方式四：使用Class.forName自动完成注册
    @Test
    public void connect04() throws Exception{
        // 使用反射加载Driver类
        Class.forName("com.mysql.jdbc.Driver");
        // 创建url，user，password
        String url = "jdbc:mysql://localhost:3306/young";
        String user = "root";
        String password = "root";
        Connection connection = DriverManager.getConnection(url, user, password);
        // 3.执行SQL
        String sql = "insert into student values(null,'marry','女','1888-8-8','222222')";
        // statement 用于执行静态SQL语句并返回其生成的结果
        Statement statement = connection.createStatement();
        int rows = statement.executeUpdate(sql);    // 是dml语句，返回的是影响的行数
        System.out.println(rows > 0 ? "成功" : "失败");
        // 4.关闭连接资源
        statement.close();
        connection.close();
    }
    // 方式五：在方式四的基础上改进，增加配文件，让连接MySQL更加灵活
    @Test
    public void connect05() throws Exception{

        // 通过Properties对象获取配置文件
        Properties properties = new Properties();
        properties.load(new FileInputStream("src\\mysql.properties"));
        String url = properties.getProperty("url");
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String driver = properties.getProperty("driver");
        // 使用反射加载Driver类
        Class.forName(driver);
        Connection connection = DriverManager.getConnection(url, user, password);
        // 执行SQL
        String sql = "delete from student  where id=5 ";
        // statement 用于执行静态SQL语句并返回其生成的结果
        Statement statement = connection.createStatement();
        int rows = statement.executeUpdate(sql);    // 是dml语句，返回的是影响的行数
        System.out.println(rows > 0 ? "成功" : "失败");
        // 4.关闭连接资源
        statement.close();
        connection.close();
    }
    }

